﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Blog.Core.IServices;
using Blog.Core.Model.ViewModels;
using Blog.Core.Model.Models;
using Blog.Core.Model.Mappers;

namespace Blog.Core.Controllers
{
    /// <summary>
    /// 广告管理
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [Authorize(Policy = "SystemOrAdmin")]
    public class AdvertisementController : Controller
    {
        readonly IAdvertisementServices advertisementServices;

        public AdvertisementController(IAdvertisementServices services)
        {
            advertisementServices = services;
        }

        /// <summary>
        /// 根据ID获取数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AdvertisementViewModel> Get(int id)
        {
            var list = await this.advertisementServices.QueryByID(id);
            return list.ToAdvertisementViewModel<AdvertisementViewModel>();
        }

        /// <summary>
        /// 根据ID获取数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpGet]
        [ApiExplorerSettings(GroupName = "v2")]
        [Route("/api/v2/[controller]")]
        public IActionResult GetV2(int id)
        {
            return Ok($"GetV2:{id}");
        }

        /// <summary>
        /// 新增广告
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> Post(AdvertisementViewModel model)
        {
            return await this.advertisementServices.Add(model.ToAdvertisementViewModel<Advertisement>());
        }
    }
}